<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Domain_Store_Memcache class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Memcache.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * A domain store for a memory-level cache using Memcache.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Domain_Store_Memcache extends Domain_Store
{

    /**
     * The Memcache object that will be used to access the memory store.
     *
     * @var     Memcache
     * @access  private
     */
    private $mc;

    /**
     * Contains keys of domain objects that have been successfully added
     * to memcache using this store.
     *
     * @var     array
     * @access  private
     */
    private $keys = array();

    /**
     * Constructor.
     *
     * @param   Memcache    $mc         The memcache object that will be used
     *                                  to access the memory store
     * @access  public
     */
    public function __construct(Memcache $mc)
    {
        $this->mc = $mc;
    }

    /**
     * @see     Domain_Store::add()
     */
    public function add($key, Domain_Object $obj)
    {
        $add = $this->mc->add($key, serialize($obj));

        if ($add) {
            $this->addKey($key);
        }
    }

    /**
     * @see     Domain_Store::set()
     */
    public function set($key, Domain_Object $obj)
    {
        $set = $this->mc->set($key, serialize($obj));

        if ($set) {
            $this->addKey($key);
        }
    }

    /**
     * @see     Domain_Store::find()
     */
    public function find($key)
    {
        $obj = $this->mc->get($key);
        return $obj ? unserialize($obj) : null;
    }

    /**
     * @see     Domain_Store::save()
     */
    public function save()
    {
        foreach ($this->keys as $key) {
            $obj = $this->find($key);
            if ($obj) {
                $obj->save();
            }
        }
    }

    /**
     * Adds a domain object key to this store's collection of keys if it's not
     * already there.
     *
     * @param   string  $key            The domain object key
     * @return  void
     * @access  private
     */
    private function addKey($key)
    {
        if (!in_array($key, $this->keys)) {
            $this->keys[] = $key;
        }
    }

}

?>
